package com.cuit.qx.controller;


import com.cuit.qx.aop.OperLog;
import com.cuit.qx.bean.Handover;
import com.cuit.qx.service.HandoverService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.thymeleaf.util.ListUtils.isEmpty;

/**
 * <p>
 * 交接班
 * </p>
 *
 * @author lcj
 * @since 2021-01-22
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/handover")
public class HandoverController {
    @Resource
    private HandoverService handoverService;


    /**
     * 查询最新交接班记录
     *
     * @param pageNo    当前页
     * @param pageSize  每页条数
     * @param name      值班人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "/getNewHandoverList", method = RequestMethod.GET)
    @OperLog(operModul = "交接班管理", operType = "1", operDesc = "查询最新交接班记录")
    @ResponseBody
    public ReturnData getNewHandoverList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         String name, String startTime, String endTime) {
        List<Handover> handoverList = handoverService.getNewHandoverList(pageNo, pageSize, name, startTime, endTime);
        int count = handoverService.getCount(name, startTime, endTime);
        if (!isEmpty(handoverList)) {
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(), ReturnStateCode.SUCCEE.getMsg(), count, handoverList);
        } else {
            return new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(), ReturnStateCode.SUCCEEBUT.getMsg(), count, handoverList);
        }
    }

    /**
     * 查询最新交接班某一个记录
     *
     * @param id 交接班id
     * @return
     */
    @RequestMapping(value = "/getNewHandover", method = RequestMethod.GET)
    @OperLog(operModul = "交接班管理", operType = "1", operDesc = "查询最新交接班某一个记录")
    @ResponseBody
    public ReturnData getNewHandover(String id) {
        Handover handover = handoverService.getNewHandover(id);
        if (handover != null) {
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(), ReturnStateCode.SUCCEE.getMsg(), handover);
        } else {
            return new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(), ReturnStateCode.SUCCEEBUT.getMsg(), handover);
        }
    }

    /**
     * 最新交接班记录变为历史交接记录
     *
     * @param id 交接班id
     * @return
     */
    @RequestMapping(value = "/updateNewHandover", method = RequestMethod.GET)
    @OperLog(operModul = "交接班管理", operType = "3", operDesc = "最新交接班记录变为历史交接记录")
    @ResponseBody
    public ReturnData updateNewHandover(String id) {
        int num = handoverService.updateNewHandover(id);
        if (num > 0) {
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(), ReturnStateCode.SUCCEE.getMsg());
        } else {
            return new ReturnData(ReturnStateCode.FAILED.getCode(), ReturnStateCode.FAILED.getMsg());
        }
    }


    /**
     * 新增交接记录
     * @param handover
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @OperLog(operModul = "交接班信息", operType = "2", operDesc = "新增交接班记录")
    public ReturnData addHandover(@RequestBody Handover handover){
        Boolean save = handoverService.addHandover(handover);
        if (save){
            return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg());
        }else {
            return new ReturnData(ReturnStateCode.FAILED.getCode(),ReturnStateCode.FAILED.getMsg());
        }
    }

    /**
     * 查询历史交接记录
     * @return
     */
    @RequestMapping(value = "/hisList",method = RequestMethod.GET)
    @OperLog(operModul = "交接班信息",operType = "1", operDesc = "查询历史交接班信息")
    public ReturnData hisList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        List<Handover> list = handoverService.getHisList();
        Integer count = list.size(); // 总数
        Integer pageCount = 0; // 页数

        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0;
        int toIndex = 0;

        if (pageNo != pageCount) {
            fromIndex = (pageNo - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNo - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);
        if (list.size()==0){
            return new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(),ReturnStateCode.SUCCEEBUT.getMsg());
        }
        return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg(),count,pageList);
    }

    /**
     * 修改历史交接记录
     * @return
     */
    @RequestMapping(value = "/updateHisId",method = RequestMethod.PUT)
    @OperLog(operModul = "交接班信息",operType = "3", operDesc = "查询历史交接班信息")
    public ReturnData updateHisById(@RequestBody Handover handover){
        Boolean update = handoverService.updateHisById(handover);
        if (!update){
            return new ReturnData(ReturnStateCode.FAILED.getCode(),ReturnStateCode.FAILED.getMsg());
        }
        return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg());
    }


}
